package com.atguigu.ssyx.search.controller;

import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.search.SkuEs;
import com.atguigu.ssyx.search.service.SkuService;
import com.atguigu.ssyx.vo.search.SkuEsQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商品搜索列表接口
 * </p>
 */
@RestController
@Api(tags = "商品接口")
@RequestMapping("/api/search/sku")
public class SkuApiController {

    @Autowired
    private SkuService skuService;


    @ApiOperation(value = "上架商品")
    @GetMapping("inner/upperSku/{skuId}")
//    商品id.
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        skuService.upperSku(skuId);
        return Result.ok();
    }


    @ApiOperation(value = "下架商品")
    @GetMapping("inner/lowerSku/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        skuService.lowerSku(skuId);
        return Result.ok();
    }

    @ApiOperation(value = "获得热门商品")
    @GetMapping("inner/findHotSkuList")
    public List<SkuEs> findHotSkuList() {
        List<SkuEs> hotSkuList = skuService.findHotSkuList();
        return hotSkuList;
    }


    //    查询分类接口\
    @GetMapping("{page}/{limit}")
    public Result listSku(@PathVariable("page") Integer page, @PathVariable(value = "limit") Integer limit, SkuEsQueryVo skuEsQueryVo) {
//        1.创建es的分页对象 Pageable pageable =Pagerequest.of()
        Pageable pageable = PageRequest.of(page - 1, limit);
//        在es查找出来数据.
        List<SkuEs> skuEsPage = skuService.search(pageable, skuEsQueryVo);
        return Result.ok(skuEsPage);
    }


    /**
     * 更新商品热度
     *
     * @return
     */
    @GetMapping("inner/incrHOtScore/{skuId}")
    public Boolean updateHots(@PathVariable Long skuId) {
        skuService.incrHotScore(skuId);
        return true;
    }

}